/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.examples.nmea.data;

import java.io.DataInput;
import java.io.IOException;

import de.thwildau.tm.moses.abnf.io.AbnfCoder;
import de.thwildau.tm.moses.abnf.io.Transmitable;
import de.thwildau.tm.moses.abnf.io.ValueEnum;

public abstract class Nmea implements Transmitable {
    
    public enum Type implements ValueEnum {
        
        GPGGA("GPGGA"),
        GPRMC("GPRMC"),
        GPGLL("GPGLL"),
        RPYL("RPYL"),
        APPOWER("APPOWER"),;
        
        private final String value;
        
        private Type(String value) {
            this.value = value;
        }
        
        @Override
        public String getValue() {
            return value;
        }
    }
 
    protected Integer checksum;

    public final Integer getChecksum() {
        return checksum;
    }
    
    @SuppressWarnings("deprecation")
    public static Nmea readObject(DataInput in) throws IOException {
        AbnfCoder.checkDecode(in, "$");
        Type type = AbnfCoder.decode(in, Type.values());
        Nmea nmea = null;
        AbnfCoder.checkDecode(in, ",");
        switch (type) {
            case APPOWER:
                nmea = new Appower();
                break;
            case GPGGA:
                nmea = new GpGga();
                break;
            case GPRMC:
                nmea = new GpRmc();
                break;
            case GPGLL:
                nmea = new GpGll();
                break;
            case RPYL:
                nmea = new Rpyl();
                break;
            default:
                throw new IOException("Unknown NMEA value '" + type + "'.");
        }
        nmea.read(in);
        return nmea;
    }
}
